(function (vc) {
    var DEFAULT_ROWS = 10
    vc.extends({
        propTypes: {
            emitListener: vc.propTypes.string,
            emitFunction: vc.propTypes.string
        },
        data: {
            addOrgCommunityInfo: {
                communitys: [],
                communityName: '',
                orgId: '',
                orgName: '',
                selectCommunitys: []
            }
        },
        watch: { // 监视双向绑定的数据数组
            checkData: {
                handler() { // 数据数组有变化将触发此函数
                    if ($that.addOrgCommunityInfo.selectCommunitys.length == $that.addOrgCommunityInfo.communitys.length) {
                        document.querySelector('#quan').checked = true;
                    } else {
                        document.querySelector('#quan').checked = false;
                    }
                },
                deep: true // 深度监视
            }
        },
        _initMethod: function () {
        },
        _initEvent: function () {
            vc.on('addOrgCommunity', 'openAddOrgCommunityModal', function (_param) {
                $that._refreshChooseOrgInfo();
                $('#addOrgCommunityModel').modal('show');
                vc.copyObject(_param, $that.addOrgCommunityInfo);
                $that._loadAllCommunityInfo(1, 10, '');
            });

            vc.on('addOrgCommunity', 'paginationPlus', 'page_event', function (_currentPage) {
                $that._loadAllCommunityInfo(_currentPage, DEFAULT_ROWS);
            });
        },
        methods: {
            _loadAllCommunityInfo: function (_page, _row, _name) {
                var param = {
                    params: {
                        page: _page,
                        row: _row,
                        name: _name,
                        orgId: $that.addOrgCommunityInfo.orgId
                    }
                };
                //发送get请求
               vc.http.apiGet('/shop.queryShop',
                             param,
                             function(json){
                                var _communityInfo = JSON.parse(json);
                                $that.addOrgCommunityInfo.communitys = _communityInfo.data;
                                vc.emit('addOrgCommunity','paginationPlus', 'init', {
                                    total: _communityInfo.records,
                                    currentPage: _page
                                });
                             },function(){
                                console.log('请求失败处理');
                             }
                           );
            },
            addOrgCommunity: function (_org) {
                var _selectCommunitys = $that.addOrgCommunityInfo.selectCommunitys;
                var _tmpCommunitys = $that.addOrgCommunityInfo.communitys;
                if (_selectCommunitys.length < 1) {
                    vc.toast("请选择隶属小区");
                    return;
                }
                var _communitys = [];
                for(var _selectIndex = 0 ;_selectIndex <_selectCommunitys.length ;_selectIndex ++){
                    for(var _communityIndex =0; _communityIndex < _tmpCommunitys.length;_communityIndex++){
                        if(_selectCommunitys[_selectIndex] == _tmpCommunitys[_communityIndex].shopId){
                            _communitys.push({
                                communityId:_tmpCommunitys[_communityIndex].shopId,
                                communityName:_tmpCommunitys[_communityIndex].shopName
                            });
                        }
                    }
                }
                var _objData = {
                    orgId: $that.addOrgCommunityInfo.orgId,
                    orgName: $that.addOrgCommunityInfo.orgName,
                    communitys: _communitys
                }
                vc.http.post('addOrgCommunity',
                    'save',
                    JSON.stringify(_objData),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        $('#addOrgCommunityModel').modal('hide');
                        if (res.status == 200) {
                            vc.emit($props.emitListener, $props.emitFunction, {});
                            vc.toast("添加成功");
                            return;
                        }
                        vc.toast(json);
                    }, function () {
                        console.log('请求失败处理');
                    }
                );
                $('#addOrgCommunityModel').modal('hide');
            },
            queryCommunitys: function () {
                $that._loadAllCommunityInfo(1, 10, $that.addOrgCommunityInfo.communityName);
            },
            _refreshChooseOrgInfo: function () {
                $that.addOrgCommunityInfo = {
                    communitys: [],
                    communityName: '',
                    orgId: '',
                    orgName: '',
                    selectCommunitys: []
                };
            },
            checkAll: function (e) {
                var checkObj = document.querySelectorAll('.checkItem'); // 获取所有checkbox项
                if (e.target.checked) { // 判定全选checkbox的勾选状态
                    for (var i = 0; i < checkObj.length; i++) {
                        if (!checkObj[i].checked) { // 将未勾选的checkbox选项push到绑定数组中
                            $that.addOrgCommunityInfo.selectCommunitys.push(checkObj[i].value);
                        }
                    }
                } else { // 如果是去掉全选则清空checkbox选项绑定数组
                    $that.addOrgCommunityInfo.selectCommunitys = [];
                }
            }
        }
    });
})(window.vc);
